package com.maxlen.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maxlen.admin.entity.RolePermission;
import com.maxlen.admin.mapper.RolePermissionMapper;
import com.maxlen.admin.service.IRolePermissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-10-11
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService {

    @Override
    @Transactional
    public void addByPermissionIdList(Long roleId, List<Long> permissionId) {
        for (Long num : permissionId) {
            baseMapper.insert(new RolePermission(roleId, num));
        }
    }

    @Override
    @Transactional
    public void removeByRoleId(Long id) {
        baseMapper.delete(new LambdaQueryWrapper<RolePermission>().eq(RolePermission::getRoleId, id));
    }

    @Override
    @Transactional
    public void removeByPermissionId(Long id) {
        baseMapper.delete(new LambdaQueryWrapper<RolePermission>().eq(RolePermission::getPermissionId, id));
    }

    @Override
    @Transactional
    public void removeByPermissionIdRoleId(Long roleId, Long permissionId) {
        baseMapper.delete(new LambdaQueryWrapper<RolePermission>().eq(RolePermission::getRoleId, roleId).eq(RolePermission::getPermissionId, permissionId));
    }
}
